
//
//  FileViewController.m
//  YiChat20160706
//
//  Created by 李政 on 16/7/6.
//  Copyright © 2016年 ZhiYou. All rights reserved.
//

#import "FileViewController.h"

#import "LocalFileViewController.h"

#import "PublicView.h"
#import "PersonalView.h"

#import "DownloadManager.h"

@interface FileViewController ()

{
    PublicView *pubView;
}
@end

@implementation FileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.navigationItem.titleView = [self createSegmet];
    
    _bgScroll.contentSize = CGSizeMake(SCREEN_WIDTH * 2, SCREEN_HEIGHT);
    
    _bgScroll.directionalLockEnabled = YES;
    
    _bgScroll.pagingEnabled = YES;
    
    _bgScroll.backgroundColor = [UIColor yellowColor];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"download_button"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButton)];
    self.navigationItem.rightBarButtonItem = barButton;
    
    
    pubView = [[PublicView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_BAR_HEIGHT - TAB_BAR_HEIGHT)];
    
    [_bgScroll addSubview:pubView];
    
    
    
    PersonalView *personalView = [[PersonalView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_BAR_HEIGHT - TAB_BAR_HEIGHT)];
//    personalView.backgroundColor = [UIColor blueColor];
    [_bgScroll addSubview:personalView];
    
    
    [self loadData];
}


- (void)rightBarButton
{
    LocalFileViewController *localVC = [[LocalFileViewController alloc] init];
    
    localVC.block = ^
    {
        [pubView reloadCollectionData];
    };
    
    [self.navigationController pushViewController:localVC animated:YES];
}

- (void)loadData
{
    AFHTTPSessionManager *mgr = [NetTool getSessionManager];
    
    if (mgr)
    {
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        NSString *accessToken = [userDefaults objectForKey:ACCESS_TOKEN];
        
        
        NSDictionary *paramaterDic = @{
                                       @"command":@"ST_F_FL",
                                       @"access_token":accessToken
                                       };
        
        [mgr GET:[self getNormalInterfaceUrl] parameters:paramaterDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if (responseObject)
            {
                
                [NetTool hideIndicator];
                
                NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
                if ([resultDic[@"error"] isEqualToString:@""])
                {
                    //弹出提示框
                }
                else
                {
                    //成功获取到数据，刷新页面
                    
                    
                    pubView.dataArray = resultDic[@"filelist"][@"pub_file"];
                    
                    //这个是公共资源的检查。还有个人资源同样需要去检查
                    
                    //检查获取到的文件数据是否为已经下载过的，如果是已经下载过的。全部添加到已经下载过的数组中去。
                    for (int i = 0;  i < pubView.dataArray.count; i ++)
                    {
                        NSDictionary *dic = pubView.dataArray[i];
                        
                        
                        if ([Util checkTheFileDidDownloadWithFile:dic])
                        {
                            [[DownloadManager shared] addObject:dic];
                        }
                    }
                    
                    
                    [pubView reloadCollectionData];
                }
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
}


- (UISegmentedControl *)createSegmet
{
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"公共资源",@"个人资源"]];
    
    [segment addTarget:self action:@selector(segmentControlClicked:) forControlEvents:UIControlEventValueChanged];
    
    segment.selectedSegmentIndex = 0;
    
    return segment;
}

- (void)segmentControlClicked:(id)sender
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
